using System;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.ComponentModel;
using System.ComponentModel.Design;
using CodeSmith.Engine;

public class BusinessDataCatalog : CodeTemplate
{
	private string _outputDirectory = String.Empty;
    private string _outputFileName = String.Empty;
    
	[Editor(typeof(System.Windows.Forms.Design.FolderNameEditor), typeof(System.Drawing.Design.UITypeEditor))] 
	[Optional]
	[Category("Output")]
	[Description("The directory to output the results to.")]
	public string OutputDirectory 
	{
		get
		{
			// default to the directory that the template is located in
			if (_outputDirectory.Length == 0) return this.CodeTemplateInfo.DirectoryName + "output\\";
			
			return _outputDirectory;
		}
		set
		{
			if (!value.EndsWith("\\")) value += "\\";
			_outputDirectory = value;
		} 
	}
    [Optional]
	[Category("Output")]
	[Description("The filename of the XML file.")]
    public string OutputFileName
    {
        get
        {
            return _outputFileName;
        }
        set
        {
            _outputFileName = value;
        }
    }
    
	public void OutputTemplate(CodeTemplate template)
	{
		this.CopyPropertiesTo(template);
		template.Render(this.Response);
	}
    
	public void DeleteFiles(string directory, string searchPattern)
	{
		string[] files = Directory.GetFiles(directory, searchPattern);
		
		for (int i = 0; i < files.Length; i++)
		{
			try
			{
				File.Delete(files[i]);
			}
			catch (Exception ex)
			{
				Response.WriteLine("Error while attempting to delete file (" + files[i] + ").\r\n" + ex.Message);
			}
		}
	}
}